package scfg.utils.training_set_filter;

import java.io.*;
import java.util.*;

import scfg.AlignedSequence;
import scfg.CYK;
import scfg.Grammar;
import scfg.PfoldGrammar;
import scfg.output.Display;
import scfg.output.RNAFormattedFile;
import scfg.utils.Compare;

public class PredictionMain {
	
	public static void runPrediction(String[] args) throws FileNotFoundException {
		if (args.length != 2) {
			System.out.println("args[0] = Training Set Filename");
			System.out.println("args[1] = Output Filename");
			return;
		}
		String trainingSetFilename = args[0];
		Display output = new Display(args[1], null, false);
		Display progress = new Display("progress_"+args[1], null, false);
		File tsFile = new File(trainingSetFilename);
		Scanner scan = new Scanner(tsFile);
		List<RNAFormattedFile> rnas = new LinkedList<RNAFormattedFile>();
		while(scan.hasNextLine()){
			rnas.add(new RNAFormattedFile(new File(scan.nextLine())));
		}
		Grammar g = new PfoldGrammar();
		for (RNAFormattedFile rnaff : rnas)
			g.add(rnaff.get("nat"), rnaff.get("seq"));
		int count = rnas.size();
		for (RNAFormattedFile rnaff : rnas) {
			progress.outC("Starting: ").out((count--) + "/" + rnas.size()).flushDisplay();
			String pred = CYK.predictFromGrammar(rnaff.get("seq"), g);
			double mfe = Compare.getFMeasure(rnaff.get("nat"), rnaff.get("mfe_gt"));
			double pfold = Compare.getFMeasure(rnaff.get("nat"), pred);
			output.outC(Display.buf("" + mfe, 8, ' ')).out(Display.buf("" + pfold, 8, ' ')).flushDisplay();
		}
		output.finalizeDisplay();
		System.out.println(Display.box("  DONE  ", "\t"));
	}
	
	public static void printSets() {
//		List<AlignedSequence> _16S = Main.getApprovedSequences("16S", .87, .8);
		List<AlignedSequence> _5S = Main.getApprovedSequences("5S", .85, .8, new Display("5S"+"_filteringResults.txt"));
		
		List<AlignedSequence>[] arr = RandomizedSetSelector.getEMHTrainingSets(_5S);
		for(int i =0;i<arr.length;i++) {
			System.out.println(Display.box("" + i, "\t"));
			for(AlignedSequence seq : arr[i])
				System.out.println(seq.getName());
		}
	}

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {
		runPrediction(args);
	}

}
